﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data;

namespace Server
{
    class BattleHandler : IHandler
    {
        public void Register(Dictionary<Protocal, Action<SocketClient, ByteBuffer>> handers)
        {
            handers.Add(Protocal.PlayerMove_CMD,PlayerMove);
            handers.Add(Protocal.PlayerFire_CMD,PlayerFire);
            handers.Add(Protocal.PlayerDamage_CMD, PlayerDamage);
            handers.Add(Protocal.PlayerDead_CMD, PlayerDead);

        }


        /// <summary>
        /// 玩家位移
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        void PlayerMove(SocketClient client, ByteBuffer buffer)
        {
            ReqPosition req = ProtoHelper.Deserialize<ReqPosition>(buffer.ReadBytes());
            GameServer.Instance.SendOther(client, Protocal.PlayerMove_CMD, req);
        }

        /// <summary>
        /// 玩家发射子弹
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        void PlayerFire(SocketClient client, ByteBuffer buffer)
        {
            ReqPosition req = ProtoHelper.Deserialize<ReqPosition>(buffer.ReadBytes());
            GameServer.Instance.SendOther(client, Protocal.PlayerFire_CMD, req);
        }

        /// <summary>
        /// 玩家受伤
        /// </summary>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        private void PlayerDamage(SocketClient client, ByteBuffer buffer)
        {
            ReqHit hit = ProtoHelper.Deserialize<ReqHit>(buffer.ReadBytes());

            Account hurt = ActorManager.Instance.GetActor(hit.hurtname);
            hurt.hp--;
            if (hurt.hp <= 0)
            {
                hit.hp = 0;
                GameServer.Instance.SendAll(Protocal.PlayerDead_CMD, hit);
            }
            else
            {
                hit.hp = hurt.hp;
                GameServer.Instance.SendAll(Protocal.PlayerDamage_CMD, hit);
            }
        }


        /// <summary>
        /// 玩家死亡
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void PlayerDead(SocketClient client, ByteBuffer buffer)
        {
            
        }
    }
}
